package Model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import Information.Apply;
import Information.News;

public class ApplyDAO{
	
	private String UName;
	private String UUser;
	private String EName;
	private String EUser;
	private String HireCompany;
	private String Job;
	
	public ApplyDAO()
	{
		this.UName="";
		this.UUser="";
		this.EName="";
		this.EUser="";
		this.HireCompany="";
		this.Job="";
	}
	
	public boolean AddNewApply(int UNO,String UName,String UUser,String EName,String EUser,String HireCompany,String Job) throws SQLException, ClassNotFoundException
	{
		int ENO = 0;
		String sql1 = "select ENO from Enterprise where EUser='"+EUser+"';";
		Connection con = null;
		Statement stmt = null;
		ResultSet resultSet = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		resultSet = stmt.executeQuery(sql1);
		while(resultSet.next())
		{
			ENO = resultSet.getInt("ENO");
		}
		String sql ="insert into Apply (UNO,ENO,UName,UUser,EName,EUser,HireCompany,Job) values ('"+UNO+"','"+ENO+"','"+UName+"','"+UUser+"','"+EName+"','"+EUser+"','"+HireCompany+"','"+Job+"')";
		if(stmt.executeUpdate(sql)==1)
		{
			con.close();
			return true;
		}
		else
		{
			con.close();
			return false;
		}
	}
	
	public ArrayList<Apply> SeeApplyByPerson(String UUser) throws SQLException, ClassNotFoundException
	{
		ArrayList<Apply> arrayList = null;
		Apply Apply = new Apply();
		String sql ="select * from Apply where UUser ='"+UUser+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		UName = result.getString("UName");
		EName = result.getString("EName");
		EUser = result.getString("EUser");
		HireCompany = result.getString("HireCompany");
		Job = result.getString("Job");
		Apply.SetApplyInfo(UName, UUser, EName, EUser, HireCompany, Job);
		arrayList.add(Apply);
		return arrayList;
	}
	
	public ArrayList<Apply> SeeApplyByEnterprise(String EUser) throws SQLException, ClassNotFoundException
	{
		ArrayList<Apply> arrayList = new ArrayList<Apply>();
		String sql ="select * from Apply where EUser ='"+EUser+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		while(result.next())
		{
			Apply apply = new Apply();
			UName = result.getString("UName");
			UUser = result.getString("UUser");
			EName = result.getString("EName");
			HireCompany = result.getString("HireCompany");
			Job = result.getString("Job");
//			System.out.println(UName);
//			System.out.println(Job);
			apply.SetApplyInfo(UName, UUser, EName, EUser, HireCompany, Job);
			arrayList.add(apply);
		}
		return arrayList;
	}
	
	public boolean DeleteApplyByPerson(String UUser) throws ClassNotFoundException, SQLException
	{
		String sql ="delete from Apply where UUser='"+UUser+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean DeleteApplyByEnterprise(String EUser) throws ClassNotFoundException, SQLException
	{
		String sql ="delete from Apply where EUser='"+EUser+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean UpdateApply(String UName,String UUser,String EName,String EUser,String HireCompany,String Job) throws ClassNotFoundException, SQLException
	{
		//can not update EUser
		String sql ="update Apply set UName='"+UName+"',set UUser='"+UUser+"',set EName='"+EName+"',set EUser='"+EUser+"',set HireCompany='"+HireCompany+"',set Job='"+Job+"'where UUser='"+UUser+"'and EUser='"+EUser+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;//update successfully
		}
		else
		{
			return false;//update failed
		}
	}
}